import {Component} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";
import {ConfigProvider} from "../../providers/config/config";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage()
@Component({
    selector: 'page-member',
    templateUrl: 'member.html',
})
export class MemberPage {
    showName = [];
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider,
        public config: ConfigProvider,
        public util: UtilsProvider,
        public loadingCtrl: LoadingController
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    /**
     * 进入页面时检查本地是否有用户Token信息
     * 如果有直接读取，
     * 如果没有则请求
     * */
    userInfo = {};
    ionViewWillEnter(){
        if(this.validate.checkLogin()){
            let userInfo = this.storage.get('userInfo'); //从本地获取userInfo信息            
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo);
            } else {
                this.getUserInfo();
            }
        }else{
            this.validate.getWxInfo(data=>{});
        }
    }


    /**
     * 通过token请求用户信息
     * 如果本地有token，则直接请求用户信息
     * 如果没有token，则微信授权登录
     * */
    getUserInfo() {
        let tokenInfo = this.storage.get('tokenInfo');
        if (tokenInfo) {
            tokenInfo = JSON.parse(tokenInfo);
            this.httpService.requestData('/user_info/index/token/' + tokenInfo['token'], data => {
                this.userInfo = data['data']['user_base'];
                
                this.storage.set('userInfo', JSON.stringify(this.userInfo));
            });
        }
    }

    /**
     * 退出清除本地数据
     * */
    logout() {
        this.storage.clear();
        alert('clear success');
        // this.navCtrl.popToRoot({
        //     animate: true,
        //     direction: 'forward'
        // });
    }

    goPage(page, params){
        let loading = this.util.loading();
        loading.show();
        this.navCtrl.push(page, params).then(success=>{
            if(success){
                loading.close();
            }
        });
    }
    dropDown(name){
        if(this.util.inArray(this.showName,name)>-1){
            this.showName.splice(this.showName.indexOf(name),1)
        }else{
            this.showName.push(name)
        }
        
    }
    pageRedirect(page,data={}){
        this.navCtrl.push(page,data).then(success=>{
            if(success !== true){//页面需要登录后跳转
                this.navCtrl.push('LoginPage',data);
            }
        })
    }

}
